home *** CD-ROM | disk | FTP | other *** search
-
-
-
- PERLLOL(1) User Contributed Perl Documentation PERLLOL(1)
-
-
- NNNNAAAAMMMMEEEE
- perlLoL - Manipulating Lists of Lists in Perl
-
- DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
- DDDDeeeeccccllllaaaarrrraaaattttiiiioooonnnn aaaannnndddd AAAAcccccccceeeessssssss ooooffff LLLLiiiissssttttssss ooooffff LLLLiiiissssttttssss
- The simplest thing to build is a list of lists (sometimes
- called an array of arrays). It's reasonably easy to
- understand, and almost everything that applies here will
- also be applicable later on with the fancier data
- structures.
-
- A list of lists, or an array of an array if you would, is
- just a regular old array @@@@LLLLooooLLLL that you can get at with two
- subscripts, like $$$$LLLLooooLLLL[3][2]. Here's a declaration of the
- array:
-
- #### aaaassssssssiiiiggggnnnn ttttoooo oooouuuurrrr aaaarrrrrrrraaaayyyy aaaa lllliiiisssstttt ooooffff lllliiiisssstttt rrrreeeeffffeeeerrrreeeennnncccceeeessss
- @@@@LLLLooooLLLL ==== ((((
- [[[[ """"ffffrrrreeeedddd"""",,,, """"bbbbaaaarrrrnnnneeeeyyyy"""" ]]]],,,,
- [[[[ """"ggggeeeeoooorrrrggggeeee"""",,,, """"jjjjaaaannnneeee"""",,,, """"eeeellllrrrrooooyyyy"""" ]]]],,,,
- [[[[ """"hhhhoooommmmeeeerrrr"""",,,, """"mmmmaaaarrrrggggeeee"""",,,, """"bbbbaaaarrrrtttt"""" ]]]],,,,
- ))));;;;
-
- pppprrrriiiinnnntttt $$$$LLLLooooLLLL[[[[2222]]]][[[[2222]]]];;;;
- bbbbaaaarrrrtttt
-
- Now you should be very careful that the outer bracket type
- is a round one, that is, parentheses. That's because
- you're assigning to an @@@@lllliiiisssstttt, so you need parens. If you
- wanted there _n_o_t to be an @@@@LLLLooooLLLL, but rather just a
- reference to it, you could do something more like this:
-
- #### aaaassssssssiiiiggggnnnn aaaa rrrreeeeffffeeeerrrreeeennnncccceeee ttttoooo lllliiiisssstttt ooooffff lllliiiisssstttt rrrreeeeffffeeeerrrreeeennnncccceeeessss
- $$$$rrrreeeeffff____ttttoooo____LLLLooooLLLL ==== [[[[
- [[[[ """"ffffrrrreeeedddd"""",,,, """"bbbbaaaarrrrnnnneeeeyyyy"""",,,, """"ppppeeeebbbbbbbblllleeeessss"""",,,, """"bbbbaaaammmmbbbbaaaammmm"""",,,, """"ddddiiiinnnnoooo"""",,,, ]]]],,,,
- [[[[ """"hhhhoooommmmeeeerrrr"""",,,, """"bbbbaaaarrrrtttt"""",,,, """"mmmmaaaarrrrggggeeee"""",,,, """"mmmmaaaaggggggggiiiieeee"""",,,, ]]]],,,,
- [[[[ """"ggggeeeeoooorrrrggggeeee"""",,,, """"jjjjaaaannnneeee"""",,,, """"aaaallllrrrrooooyyyy"""",,,, """"jjjjuuuuddddyyyy"""",,,, ]]]],,,,
- ]]]];;;;
-
- pppprrrriiiinnnntttt $$$$rrrreeeeffff____ttttoooo____LLLLooooLLLL---->>>>[[[[2222]]]][[[[2222]]]];;;;
-
- Notice that the outer bracket type has changed, and so our
- access syntax has also changed. That's because unlike C,
- in perl you can't freely interchange arrays and references
- thereto. $$$$rrrreeeeffff____ttttoooo____LLLLooooLLLL is a reference to an array, whereas
- @@@@LLLLooooLLLL is an array proper. Likewise, $$$$LLLLooooLLLL[2] is not an
- array, but an array ref. So how come you can write these:
-
- $$$$LLLLooooLLLL[[[[2222]]]][[[[2222]]]]
- $$$$rrrreeeeffff____ttttoooo____LLLLooooLLLL---->>>>[[[[2222]]]][[[[2222]]]]
-
- instead of having to write these:
-
-
-
-
-
- 23/Jan/96 perl 5.002 with 1
-
-
-
-
-
- PERLLOL(1) User Contributed Perl Documentation PERLLOL(1)
-
-
- $$$$LLLLooooLLLL[[[[2222]]]]---->>>>[[[[2222]]]]
- $$$$rrrreeeeffff____ttttoooo____LLLLooooLLLL---->>>>[[[[2222]]]]---->>>>[[[[2222]]]]
-
- Well, that's because the rule is that on adjacent brackets
- only (whether square or curly), you are free to omit the
- pointer dereferencing array. But you need not do so for
- the very first one if it's a scalar containing a
- reference, which means that $$$$rrrreeeeffff____ttttoooo____LLLLooooLLLL always needs it.
-
- GGGGrrrroooowwwwiiiinnnngggg YYYYoooouuuurrrr OOOOwwwwnnnn
- That's all well and good for declaration of a fixed data
- structure, but what if you wanted to add new elements on
- the fly, or build it up entirely from scratch?
-
- First, let's look at reading it in from a file. This is
- something like adding a row at a time. We'll assume that
- there's a flat file in which each line is a row and each
- word an element. If you're trying to develop an @@@@LLLLooooLLLL list
- containing all these, here's the right way to do that:
-
- wwwwhhhhiiiilllleeee ((((<<<<>>>>)))) {{{{
- @@@@ttttmmmmpppp ==== sssspppplllliiiitttt;;;;
- ppppuuuusssshhhh @@@@LLLLooooLLLL,,,, [[[[ @@@@ttttmmmmpppp ]]]];;;;
- }}}}
-
- You might also have loaded that from a function:
-
- ffffoooorrrr $$$$iiii (((( 1111 ........ 11110000 )))) {{{{
- $$$$LLLLooooLLLL[[[[$$$$iiii]]]] ==== [[[[ ssssoooommmmeeeeffffuuuunnnncccc(((($$$$iiii)))) ]]]];;;;
- }}}}
-
- Or you might have had a temporary variable sitting around
- with the list in it.
-
- ffffoooorrrr $$$$iiii (((( 1111 ........ 11110000 )))) {{{{
- @@@@ttttmmmmpppp ==== ssssoooommmmeeeeffffuuuunnnncccc(((($$$$iiii))));;;;
- $$$$LLLLooooLLLL[[[[$$$$iiii]]]] ==== [[[[ @@@@ttttmmmmpppp ]]]];;;;
- }}}}
-
- It's very important that you make sure to use the [[[[]]]] list
- reference constructor. That's because this will be very
- wrong:
-
- $$$$LLLLooooLLLL[[[[$$$$iiii]]]] ==== @@@@ttttmmmmpppp;;;;
-
- You see, assigning a named list like that to a scalar just
- counts the number of elements in @@@@ttttmmmmpppp, which probably
- isn't what you want.
-
- If you are running under uuuusssseeee ssssttttrrrriiiicccctttt, you'll have to add
- some declarations to make it happy:
-
-
-
-
-
-
- 23/Jan/96 perl 5.002 with 2
-
-
-
-
-
- PERLLOL(1) User Contributed Perl Documentation PERLLOL(1)
-
-
- uuuusssseeee ssssttttrrrriiiicccctttt;;;;
- mmmmyyyy((((@@@@LLLLooooLLLL,,,, @@@@ttttmmmmpppp))));;;;
- wwwwhhhhiiiilllleeee ((((<<<<>>>>)))) {{{{
- @@@@ttttmmmmpppp ==== sssspppplllliiiitttt;;;;
- ppppuuuusssshhhh @@@@LLLLooooLLLL,,,, [[[[ @@@@ttttmmmmpppp ]]]];;;;
- }}}}
-
- Of course, you don't need the temporary array to have a
- name at all:
-
- wwwwhhhhiiiilllleeee ((((<<<<>>>>)))) {{{{
- ppppuuuusssshhhh @@@@LLLLooooLLLL,,,, [[[[ sssspppplllliiiitttt ]]]];;;;
- }}}}
-
- You also don't have to use _p_u_s_h_(_). You could just make a
- direct assignment if you knew where you wanted to put it:
-
- mmmmyyyy ((((@@@@LLLLooooLLLL,,,, $$$$iiii,,,, $$$$lllliiiinnnneeee))));;;;
- ffffoooorrrr $$$$iiii (((( 0000 ........ 11110000 ))))
- $$$$lllliiiinnnneeee ==== <<<<>>>>;;;;
- $$$$LLLLooooLLLL[[[[$$$$iiii]]]] ==== [[[[ sssspppplllliiiitttt '''' '''',,,, $$$$lllliiiinnnneeee ]]]];;;;
- }}}}
-
- or even just
-
- mmmmyyyy ((((@@@@LLLLooooLLLL,,,, $$$$iiii))));;;;
- ffffoooorrrr $$$$iiii (((( 0000 ........ 11110000 ))))
- $$$$LLLLooooLLLL[[[[$$$$iiii]]]] ==== [[[[ sssspppplllliiiitttt '''' '''',,,, <<<<>>>> ]]]];;;;
- }}}}
-
- You should in general be leary of using potential list
- functions in a scalar context without explicitly stating
- such. This would be clearer to the casual reader:
-
- mmmmyyyy ((((@@@@LLLLooooLLLL,,,, $$$$iiii))));;;;
- ffffoooorrrr $$$$iiii (((( 0000 ........ 11110000 ))))
- $$$$LLLLooooLLLL[[[[$$$$iiii]]]] ==== [[[[ sssspppplllliiiitttt '''' '''',,,, ssssccccaaaallllaaaarrrr((((<<<<>>>>)))) ]]]];;;;
- }}}}
-
- If you wanted to have a $$$$rrrreeeeffff____ttttoooo____LLLLooooLLLL variable as a
- reference to an array, you'd have to do something like
- this:
-
- wwwwhhhhiiiilllleeee ((((<<<<>>>>)))) {{{{
- ppppuuuusssshhhh @@@@$$$$rrrreeeeffff____ttttoooo____LLLLooooLLLL,,,, [[[[ sssspppplllliiiitttt ]]]];;;;
- }}}}
-
- Actually, if you were using strict, you'd not only have to
- declare $$$$rrrreeeeffff____ttttoooo____LLLLooooLLLL as you had to declare @@@@LLLLooooLLLL, but you'd
- _a_l_s_o having to initialize it to a reference to an empty
- list. (This was a bug in 5.001m that's been fixed for the
- 5.002 release.)
-
-
-
-
-
- 23/Jan/96 perl 5.002 with 3
-
-
-
-
-
- PERLLOL(1) User Contributed Perl Documentation PERLLOL(1)
-
-
- mmmmyyyy $$$$rrrreeeeffff____ttttoooo____LLLLooooLLLL ==== [[[[]]]];;;;
- wwwwhhhhiiiilllleeee ((((<<<<>>>>)))) {{{{
- ppppuuuusssshhhh @@@@$$$$rrrreeeeffff____ttttoooo____LLLLooooLLLL,,,, [[[[ sssspppplllliiiitttt ]]]];;;;
- }}}}
-
- Ok, now you can add new rows. What about adding new
- columns? If you're just dealing with matrices, it's often
- easiest to use simple assignment:
-
- ffffoooorrrr $$$$xxxx ((((1111 ........ 11110000)))) {{{{
- ffffoooorrrr $$$$yyyy ((((1111 ........ 11110000)))) {{{{
- $$$$LLLLooooLLLL[[[[$$$$xxxx]]]][[[[$$$$yyyy]]]] ==== ffffuuuunnnncccc(((($$$$xxxx,,,, $$$$yyyy))));;;;
- }}}}
- }}}}
-
- ffffoooorrrr $$$$xxxx (((( 3333,,,, 7777,,,, 9999 )))) {{{{
- $$$$LLLLooooLLLL[[[[$$$$xxxx]]]][[[[22220000]]]] ++++==== ffffuuuunnnncccc2222(((($$$$xxxx))));;;;
- }}}}
-
- It doesn't matter whether those elements are already there
- or not: it'll gladly create them for you, setting
- intervening elements to uuuunnnnddddeeeeffff as need be.
-
- If you just wanted to append to a row, you'd have to do
- something a bit funnier looking:
-
- #### aaaadddddddd nnnneeeewwww ccccoooolllluuuummmmnnnnssss ttttoooo aaaannnn eeeexxxxiiiissssttttiiiinnnngggg rrrroooowwww
- ppppuuuusssshhhh @@@@{{{{ $$$$LLLLooooLLLL[[[[0000]]]] }}}},,,, """"wwwwiiiillllmmmmaaaa"""",,,, """"bbbbeeeettttttttyyyy"""";;;;
-
- Notice that I _c_o_u_l_d_n_'_t just say:
-
- ppppuuuusssshhhh $$$$LLLLooooLLLL[[[[0000]]]],,,, """"wwwwiiiillllmmmmaaaa"""",,,, """"bbbbeeeettttttttyyyy"""";;;; #### WWWWRRRROOOONNNNGGGG!!!!
-
- In fact, that wouldn't even compile. How come? Because
- the argument to _p_u_s_h_(_) must be a real array, not just a
- reference to such.
-
- AAAAcccccccceeeessssssss aaaannnndddd PPPPrrrriiiinnnnttttiiiinnnngggg
- Now it's time to print your data structure out. How are
- you going to do that? Well, if you only want one of the
- elements, it's trivial:
-
- pppprrrriiiinnnntttt $$$$LLLLooooLLLL[[[[0000]]]][[[[0000]]]];;;;
-
- If you want to print the whole thing, though, you can't
- just say
-
- pppprrrriiiinnnntttt @@@@LLLLooooLLLL;;;; #### WWWWRRRROOOONNNNGGGG
-
- because you'll just get references listed, and perl will
- never automatically dereference things for you. Instead,
- you have to roll yourself a loop or two. This prints the
- whole structure, using the shell-style _f_o_r_(_) construct to
- loop across the outer set of subscripts.
-
-
-
- 23/Jan/96 perl 5.002 with 4
-
-
-
-
-
- PERLLOL(1) User Contributed Perl Documentation PERLLOL(1)
-
-
- ffffoooorrrr $$$$aaaarrrreeeeffff (((( @@@@LLLLooooLLLL )))) {{{{
- pppprrrriiiinnnntttt """"\\\\tttt [[[[ @@@@$$$$aaaarrrreeeeffff ]]]],,,,\\\\nnnn"""";;;;
- }}}}
-
- If you wanted to keep track of subscripts, you might do
- this:
-
- ffffoooorrrr $$$$iiii (((( 0000 ........ $$$$####LLLLooooLLLL )))) {{{{
- pppprrrriiiinnnntttt """"\\\\tttt eeeelllltttt $$$$iiii iiiissss [[[[ @@@@{{{{$$$$LLLLooooLLLL[[[[$$$$iiii]]]]}}}} ]]]],,,,\\\\nnnn"""";;;;
- }}}}
-
- or maybe even this. Notice the inner loop.
-
- ffffoooorrrr $$$$iiii (((( 0000 ........ $$$$####LLLLooooLLLL )))) {{{{
- ffffoooorrrr $$$$jjjj (((( 0000 ........ $$$$####{{{{$$$$LLLLooooLLLL[[[[$$$$iiii]]]]}}}} )))) {{{{
- pppprrrriiiinnnntttt """"eeeelllltttt $$$$iiii $$$$jjjj iiiissss $$$$LLLLooooLLLL[[[[$$$$iiii]]]][[[[$$$$jjjj]]]]\\\\nnnn"""";;;;
- }}}}
- }}}}
-
- As you can see, it's getting a bit complicated. That's
- why sometimes is easier to take a temporary on your way
- through:
-
- ffffoooorrrr $$$$iiii (((( 0000 ........ $$$$####LLLLooooLLLL )))) {{{{
- $$$$aaaarrrreeeeffff ==== $$$$LLLLooooLLLL[[[[$$$$iiii]]]];;;;
- ffffoooorrrr $$$$jjjj (((( 0000 ........ $$$$####{{{{$$$$aaaarrrreeeeffff}}}} )))) {{{{
- pppprrrriiiinnnntttt """"eeeelllltttt $$$$iiii $$$$jjjj iiiissss $$$$LLLLooooLLLL[[[[$$$$iiii]]]][[[[$$$$jjjj]]]]\\\\nnnn"""";;;;
- }}}}
- }}}}
-
- Hm... that's still a bit ugly. How about this:
-
- ffffoooorrrr $$$$iiii (((( 0000 ........ $$$$####LLLLooooLLLL )))) {{{{
- $$$$aaaarrrreeeeffff ==== $$$$LLLLooooLLLL[[[[$$$$iiii]]]];;;;
- $$$$nnnn ==== @@@@$$$$aaaarrrreeeeffff ---- 1111;;;;
- ffffoooorrrr $$$$jjjj (((( 0000 ........ $$$$nnnn )))) {{{{
- pppprrrriiiinnnntttt """"eeeelllltttt $$$$iiii $$$$jjjj iiiissss $$$$LLLLooooLLLL[[[[$$$$iiii]]]][[[[$$$$jjjj]]]]\\\\nnnn"""";;;;
- }}}}
- }}}}
-
-
- SSSSlllliiiicccceeeessss
- If you want to get at a slide (part of a row) in a
- multidimensional array, you're going to have to do some
- fancy subscripting. That's because while we have a nice
- synonym for single elements via the pointer arrow for
- dereferencing, no such convenience exists for slices.
- (Remember, of course, that you can always write a loop to
- do a slice operation.)
-
- Here's how to do one operation using a loop. We'll assume
- an @@@@LLLLooooLLLL variable as before.
-
-
-
-
-
- 23/Jan/96 perl 5.002 with 5
-
-
-
-
-
- PERLLOL(1) User Contributed Perl Documentation PERLLOL(1)
-
-
- @@@@ppppaaaarrrrtttt ==== (((())));;;;
- $$$$xxxx ==== 4444;;;;
- ffffoooorrrr (((($$$$yyyy ==== 7777;;;; $$$$yyyy <<<< 11113333;;;; $$$$yyyy++++++++)))) {{{{
- ppppuuuusssshhhh @@@@ppppaaaarrrrtttt,,,, $$$$LLLLooooLLLL[[[[$$$$xxxx]]]][[[[$$$$yyyy]]]];;;;
- }}}}
-
- That same loop could be replaced with a slice operation:
-
- @@@@ppppaaaarrrrtttt ==== @@@@{{{{ $$$$LLLLooooLLLL[[[[4444]]]] }}}} [[[[ 7777........11112222 ]]]];;;;
-
- but as you might well imagine, this is pretty rough on the
- reader.
-
- Ah, but what if you wanted a _t_w_o_-_d_i_m_e_n_s_i_o_n_a_l _s_l_i_c_e, such
- as having $$$$xxxx run from 4..8 and $$$$yyyy run from 7 to 12? Hm...
- here's the simple way:
-
- @@@@nnnneeeewwwwLLLLooooLLLL ==== (((())));;;;
- ffffoooorrrr (((($$$$ssssttttaaaarrrrttttxxxx ==== $$$$xxxx ==== 4444;;;; $$$$xxxx <<<<==== 8888;;;; $$$$xxxx++++++++)))) {{{{
- ffffoooorrrr (((($$$$ssssttttaaaarrrrttttyyyy ==== $$$$yyyy ==== 7777;;;; $$$$xxxx <<<<==== 11112222;;;; $$$$yyyy++++++++)))) {{{{
- $$$$nnnneeeewwwwLLLLooooLLLL[[[[$$$$xxxx ---- $$$$ssssttttaaaarrrrttttxxxx]]]][[[[$$$$yyyy ---- $$$$ssssttttaaaarrrrttttyyyy]]]] ==== $$$$LLLLooooLLLL[[[[$$$$xxxx]]]][[[[$$$$yyyy]]]];;;;
- }}}}
- }}}}
-
- We can reduce some of the looping through slices
-
- ffffoooorrrr (((($$$$xxxx ==== 4444;;;; $$$$xxxx <<<<==== 8888;;;; $$$$xxxx++++++++)))) {{{{
- ppppuuuusssshhhh @@@@nnnneeeewwwwLLLLooooLLLL,,,, [[[[ @@@@{{{{ $$$$LLLLooooLLLL[[[[$$$$xxxx]]]] }}}} [[[[ 7777........11112222 ]]]] ]]]];;;;
- }}}}
-
- If you were into Schwartzian Transforms, you would
- probably have selected map for that
-
- @@@@nnnneeeewwwwLLLLooooLLLL ==== mmmmaaaapppp {{{{ [[[[ @@@@{{{{ $$$$LLLLooooLLLL[[[[$$$$____]]]] }}}} [[[[ 7777........11112222 ]]]] ]]]] }}}} 4444 ........ 8888;;;;
-
- Although if your manager accused of seeking job security
- (or rapid insecurity) through inscrutable code, it would
- be hard to argue. :-) If I were you, I'd put that in a
- function:
-
- @@@@nnnneeeewwwwLLLLooooLLLL ==== sssspppplllliiiicccceeee____2222DDDD(((( \\\\@@@@LLLLooooLLLL,,,, 4444 ====>>>> 8888,,,, 7777 ====>>>> 11112222 ))));;;;
- ssssuuuubbbb sssspppplllliiiicccceeee____2222DDDD {{{{
- mmmmyyyy $$$$llllrrrrrrrr ==== sssshhhhiiiifffftttt;;;; #### rrrreeeeffff ttttoooo lllliiiisssstttt ooooffff lllliiiisssstttt rrrreeeeffffssss!!!!
- mmmmyyyy (((($$$$xxxx____lllloooo,,,, $$$$xxxx____hhhhiiii,,,,
- $$$$yyyy____lllloooo,,,, $$$$yyyy____hhhhiiii)))) ==== @@@@____;;;;
-
- rrrreeeettttuuuurrrrnnnn mmmmaaaapppp {{{{
- [[[[ @@@@{{{{ $$$$llllrrrrrrrr---->>>>[[[[$$$$____]]]] }}}} [[[[ $$$$yyyy____lllloooo ........ $$$$yyyy____hhhhiiii ]]]] ]]]]
- }}}} $$$$xxxx____lllloooo ........ $$$$xxxx____hhhhiiii;;;;
- }}}}
-
-
- SSSSEEEEEEEE AAAALLLLSSSSOOOO
- _p_e_r_l_d_a_t_a(1), _p_e_r_l_r_e_f(1), _p_e_r_l_d_s_c(1)
-
-
-
- 23/Jan/96 perl 5.002 with 6
-
-
-
-
-
- PERLLOL(1) User Contributed Perl Documentation PERLLOL(1)
-
-
- AAAAUUUUTTTTHHHHOOOORRRR
- Tom Christiansen <tchrist@perl.com>
-
- Last udpate: Sat Oct 7 19:35:26 MDT 1995
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 23/Jan/96 perl 5.002 with 7
-
-
-